package com.atguigu.dga230315.governance.assess.calc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga230315.governance.assess.Assessor;
import com.atguigu.dga230315.governance.bean.AssessParam;
import com.atguigu.dga230315.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga230315.governance.bean.GovernanceMetric;
import com.atguigu.dga230315.meta.bean.TableMetaInfo;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component("NO_ACCESS")
public class NoAccessAssess extends Assessor {
    @Override
    protected void assess(GovernanceAssessDetail assessDetail, AssessParam assessParam) throws ParseException {
        //思路：首先获取到要考评表的最后访问时间 然后再获取到当前的时间  进行相减看差几天  再和阈值对比
        //注意点：如果最后访问时间为 2023年7月19日 23:59:00  当前时间为2023年7月20 00:01:00
        //那么这种应该是差一天的
        //解决思路：将时间都转为精确到天的时间戳
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        //获取这个表的最后访问时间
        Date tableLastAccessTimeDate = tableMetaInfo.getTableLastAccessTime();


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //在获取当前时间戳
//        Date curDate = new Date();
//        long curTime = System.currentTimeMillis();
        //获取当天的考评日期
        String assessDateStr = assessParam.getAssessDate();
        Date assessDate = sdf.parse(assessDateStr);
        //获取精确到天的时间戳
        long assessTime = assessDate.getTime() / 1000 / 60 / 60 / 24;


        //获取这个表的最后访问时间时间戳
//        long lastAccessTimeDateTime = tableLastAccessTimeDate.getTime();
        //获取精确到填得时间戳
        long lastAccessTime = tableLastAccessTimeDate.getTime() / 1000 / 60 / 60 / 24;

//        long diffDayTime = (curTime - lastAccessTimeDateTime)/1000/60/60/24;

        long diffDayTime = assessTime - lastAccessTime;
        //获取阈值
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();
        String metricParamsJsonStr = governanceMetric.getMetricParamsJson();
        //通常将json字符串 转为 Map  Bean  JSONObject
        JSONObject jsonObject = JSON.parseObject(metricParamsJsonStr);
        Integer days = jsonObject.getInteger("days");

        //判断时间间隔是否超过阈值
        if (diffDayTime>days){
            assessDetail.setAssessScore(BigDecimal.ZERO);
            assessDetail.setAssessProblem("长期无访问");
            assessDetail.setAssessComment(diffDayTime+"天无访问");
        }
    }
}
